import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.ObjectProperty;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.util.FileManager;

public class MiddlewareForAmI {

	/**
	 * @param args
	 * @throws Exception
	 */

	public static final String ex = "http://liris.cnrs.fr/smartspace.owl#";

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		// OntologyReasoner ontReasoner = new OntologyReasoner();
		// ontReasoner.runEngine("testOWL2.rules", "test2.owl");

		/* Load the ontology model */
		Model model = ModelFactory.createOntologyModel();

		model = FileManager.get().loadModel("smartspace.owl");

		OntModel infOntModel = null;
		String queryString = null;
		String nameRuleFile = null;
		String queryKeyWord = null;

		// Read Query File
		QueryEngine nQuery = new QueryEngine();
		queryString = nQuery.readQuery("smartspace.query");

		// Find key word
		MatchQueryKeyWord nMatch = new MatchQueryKeyWord();
		queryKeyWord = nMatch.getKeyWord(queryString);

		//
		/* Invoke the cascade ontology inference engine */
		OntologyReasoner ontoReasoner = new OntologyReasoner();
		nameRuleFile = ontoReasoner.chooseReasonRule(queryKeyWord);
		infOntModel = ontoReasoner.runEngine(nameRuleFile, model);
		// infModel.write(System.out);

		// OntModel infOntModel = ModelFactory.createOntologyModel();
		// File infFile = new File("testResults3.owl");
		// infOntModel.read(new FileInputStream(infFile),"");

		// /*Invoke the QueryEngine*/

		nQuery.runQuery(queryString, infOntModel);

		UpdateOntModel updateOntModel = new UpdateOntModel();

		updateOntModel.runUpdate(infOntModel);

		try {
			// FileInputStream fin =new FileInputStream(rdfFile);
			FileOutputStream fout = new FileOutputStream("testResults4.owl");

			infOntModel.write(fout);
			// infOntModel.write(fout);

		} catch (IOException e) {
			System.out.println("Exception caught" + e.getMessage());
		}

		queryString = nQuery.readQuery("smartspaceRestaurant.query");
		nQuery.runQuery(queryString, infOntModel);

		// infModel.

		/* Build a BDtree Engine */
		 DecisionTreeInferEngine dtEngine = new DecisionTreeInferEngine();
		 dtEngine.runDecisionTreeInferEngine("testRes.arff");
		 //return 1;

	}

}
